<?php
/*
+ --------------------------------------------------- +
|  Ineo Article Manager - Version 1.x
|  Copyright © 2005-2008, Dan Cryer and William McGann
+ --------------------------------------------------- +

Ineo Article Manager is free software; you can redistribute it and/or modify
it under the terms of version 3 of the GNU General Public License as published by
the Free Software Foundation.

Ineo Article Manager is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Ineo; see the file License.txt.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

*/

class installer
{
	var $xml                 = null;
	var $ipsclass            = null;
	var $installed_if_exists = 'ams_upgrade_history';
	var $type                = null;
	var $step                = null;
	var $data_dir            = null;
	var $step_dir            = null;
	
	// initialize any required classes, ipsclass, etc.
	function installer()
	{				
		// Output buffering
		ob_start();
		
		// Set up some defines:
		define( 'IPB_THIS_SCRIPT' , 'admin'   );
		define( 'IPB_LOAD_SQL'    , 'admin_queries' );

		// Create $INFO variable:
		$INFO = array();

		// load ipb init.php
		require_once( '../init.php' );

		// define cache path, incase we need it?
		define('CACHE_PATH',ROOT_PATH);

		// load all the other ipsclass stuff:
		require_once( ROOT_PATH   . "sources/ipsclass.php" );
		require_once( ROOT_PATH   . "sources/classes/class_session.php" );
		require_once( ROOT_PATH   . "conf_global.php" );
		require_once( KERNEL_PATH . "class_converge.php" );
		require_once( KERNEL_PATH . 'class_xml.php' );
		
		$this->ipsclass = new ipsclass();	

		// Set up conf_global data:
		$this->ipsclass->vars = $INFO;

		// Get things rollin':
		$this->ipsclass->init_db_connection();
		$this->ipsclass->parse_incoming();

		// Get cache...
		$this->ipsclass->cache_array = array('settings', 'group_cache', 'systemvars');
		$this->ipsclass->init_cache_setup();
		$this->ipsclass->init_load_cache( $this->ipsclass->cache_array );

		// Converge stuff.
		$this->ipsclass->converge = new class_converge( $this->ipsclass->DB );

		// Session stuff.
		$this->ipsclass->sess = new session();
		$this->ipsclass->sess->ipsclass =& $this->ipsclass;

		$this->ipsclass->initiate_ipsclass();

		$this->ipsclass->member     = $this->ipsclass->sess->authorise();
		$this->ipsclass->lastclick  = $this->ipsclass->sess->last_click;
		$this->ipsclass->location   = $this->ipsclass->sess->location;
		$this->ipsclass->session_id = $this->ipsclass->sess->session_id;
		$this->ipsclass->my_session = $this->ipsclass->sess->session_id;

		// return md5 check... I'm not gonna lie, I don't know what this does.
		$this->ipsclass->md5_check = $this->ipsclass->return_md5_check();
		
		$this->xml = new class_xml();
		$this->ipsclass->xml =& $this->xml;
		$this->xml->ipsclass =& $this->ipsclass;
	}
	
	// check if the user accessing the installer has admin cp permissions:
	function authorize()
	{		
		return $this->ipsclass->member['g_access_cp'];
	}
	
	// check if app is already installed?
	function is_installed()
	{
		$this->ipsclass->DB->query( 'SHOW TABLES LIKE \'%'.$this->installed_if_exists.'%\'' );
		$tbl_check = $this->ipsclass->DB->fetch_row();

		if( $tbl_check == false )
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	// Run install:
	function run()
	{
		if( $this->type == 'upgrade' )
		{
			$this->upgrade_run();
		}
		elseif( $this->type == 'install' )
		{
			$this->install_run();
		}
	}
	
	function install_run()
	{
		$this->status_text = '';
		$next_step = 0;
		
		if( file_exists( $this->step_dir . 'step-' . $this->step . '.php' ) )
		{
			require_once( $this->step_dir . 'step-' . $this->step . '.php' );

			$class = 'install_step_' . $this->step;

			$step = new $class( $this );
			$step->run_step();
			
			if( !$step->await_input )
			{
				$next_step = 1;
			}
		}
				
		if( $next_step == 1 )
		{
			$this->next_step();
		}		
		
		$this->next_step = $next_step;
		$this->print_page();
	}
	
	function upgrade_run()
	{
		//----
		// Set Vars:
		//----
		
		$this->status_text = '';
		$this->packs       = $this->get_upgrade_packs();
		sort( $this->packs );
		
		//----
		// Get current version:
		//----
		
		$this->ipsclass->DB->build_query( array('select' => '*', 'from' => 'ams_upgrade_history', 'order' => 'ams_version_id DESC', 'limit' => 1) );
		$this->ipsclass->DB->exec_query();
		
		if( $this->ipsclass->DB->get_num_rows() == 0 )
		{
			$this->current_version = 00000;
		}
		else
		{
			$v = $this->ipsclass->DB->fetch_row();
			$this->current_version = $v['ams_version_id'];
		}
		
		//----
		// Get the first pack we need to run:
		//----
		
		foreach( $this->packs as $id => $pack )
		{
			if( $pack <= $this->current_version )
			{
				unset( $this->packs[$id] );
			}
		}
		
		//----
		// Handle current step for the current upgrade pack!
		//----
		
		$order = array('','welcome','template','settings','database','cache','versions','done');
		
		if( count($this->packs) == 0 and $order[$this->step] != 'done' ) { $this->status_text = 'You are already up to date, there is nothing to install.'; $this->step = 'nothing to install'; }
		
		
		if( file_exists( $this->step_dir . 'upgrade_' . $order[$this->step] . '.php' ) )
		{
			require_once( $this->step_dir . 'upgrade_' . $order[$this->step] . '.php' );

			$class = 'upgrade_' . $order[$this->step];

			$step = new $class( $this );
			$step->run_step();
		}	
		
		if( !empty( $order[$this->step + 1] ) and $order[$this->step] != 'done' and $this->step != 'nothing to install' )
		{
			$this->type = 'upgrade';
			$this->next_step();
			$this->next_step = 1;
		}
		else
		{
			$this->next_step = 0;
		}
		
		$this->print_page();
	}
	
	
	function get_upgrade_packs()
	{
		$dirs = array();
		$dir = opendir( $this->data_dir );
		
		while( $item = readdir( $dir ) )
		{
			if( substr($item,0,1) == '.' ) continue;
			if( !is_dir( $this->data_dir . $item ) ) continue;
			
			$dirs[] = $item;
		}
		
		closedir( $dir );
		
		return $dirs;
	}
	
	function next_step()
	{
		if( empty( $this->refresh_override ) )
		{
			header( "Refresh: 3;url=index.php?".$this->type."=1&step=".($this->step+1) );
		}
		else
		{
			header('Refresh: 3;url=' . $this->refresh_override );
		}
	}
	
	function print_page()
	{
		$output = $this->status_text;
		require_once('sources/template.php');
	}
}

?>